#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "utils.h"
#include "harris.h"
#define  MAX_NUM_OF_CORNER_COORDS 20000  /* maximal number of coords we accept*/
#define  LINE_LENGTH 256
/*----------------------------------------------------------------------------*/
/* The function readScriptFile reads and analyses the script file. It returns */
/* the values of parameters that were set in the script file.                 */
int readScriptFile( char *scriptFileName, char *imageFileName,
                    char *cornerListFileName, float *k,
                   int *gaussMaskSize, float *sigma,
                   float *treshold){

    FILE *inp;
    char atom[LINE_LENGTH];
    if((inp = fopen( scriptFileName, "rt" )) != NULL){
      readAtom(inp,atom); if (strcmp(atom, "hartest") != 0 ) return -2;
      if (readAtom(inp,imageFileName)      != 0) return -3;
      if (readAtom(inp,cornerListFileName) != 0) return -4;
      if (readAtom(inp,atom) != 0) return  -5; *k					= atof(atom);
      if (readAtom(inp,atom) != 0) return  -6; *gaussMaskSize		= atoi(atom);
      if (readAtom(inp,atom) != 0) return  -7; *sigma				= atof(atom);
      if (readAtom(inp,atom) != 0) return  -8; *treshold    		= atof(atom);
      fclose(inp); return 0;
    } else return -1;
}
/*----------------------------------------------------------------------------*/

#define freeMem \
  if (byteImage  != NULL) free(byteImage);\
  if (floatImage != NULL) free(floatImage);\
  if (corners    != NULL) free(corners);\

int main( int argc, char **argv ) {

    char imageFileName[LINE_LENGTH], cornerListFileName[LINE_LENGTH];
    int  xImageSize, yImageSize, numImagePixels,gaussMaskSize,
         *corners, numCorners, i, problem;
    unsigned char *byteImage;

    float *floatImage,k ,sigma,treshold;

    clock_t start, stop;

    if (argc != 2){
        printf( "\nsyntax:\n\n" );
        printf( "  cortest file\n\n" );
        printf( "  File contains settings.\n\n");
        printf( "example: cortest test1.har\n\n");
        return( -1 );
    }

    printf("\n");
    printf("======================================================\n");
    printf("| Testing Harris for detecting the corners |\n");
    printf("======================================================\n");

    if ((problem = readScriptFile( argv[1], imageFileName,
       cornerListFileName, &k, &gaussMaskSize, &sigma, &treshold)) != 0 ){
           printf("  Problem with the script file '%s' (line %i).\n\n",
                     argv[1], (-problem-1)); exit(-1);
    }

    byteImage = loadImageFromTGA( imageFileName, &xImageSize, &yImageSize);
    if (byteImage == NULL){
        printf("  Problems with loading image from '%s'.\n\n", imageFileName);
        free(byteImage); exit(-1);
    }

    numImagePixels = xImageSize*yImageSize;
    floatImage = (float *)malloc(numImagePixels * sizeof(floatImage[0]));
    corners    = (int *)malloc(MAX_NUM_OF_CORNER_COORDS * sizeof(corners[0]));
    if ((floatImage == NULL)||(corners == NULL)){
        printf("  There is not enough memory for solving the problem.\n\n");
        freeMem; exit(-1);
    }
    printf("  The image from '%s' is now being processed.\n", imageFileName );
    for (i = 0; i < numImagePixels; i ++) floatImage[i] = byteImage[i];

    Harris h;
    start = clock();

    numCorners = h.detectCorners( floatImage, xImageSize, yImageSize,
      k , gaussMaskSize, sigma, treshold,
      corners, MAX_NUM_OF_CORNER_COORDS );

    stop = clock();

    if (numCorners == -1){
        printf("  There was not enough memory for solving the problem.\n\n");
        freeMem; exit(-1);}
    if (numCorners == -2){
        printf("  Warning: Not all the corners were placed to the\n\n");
        printf("  output list since the array is not big enough.\n\n");
        numCorners = MAX_NUM_OF_CORNER_COORDS / 2;
    }
    printf("  Result: %d corners were found ", numCorners);
    printf("in %g seconds.\n", (stop-start)/(float)CLOCKS_PER_SEC);

    if (saveCornerList(cornerListFileName, corners, numCorners) >= 0)
      printf("  The list of corners was saved into '%s'.\n",cornerListFileName);
    else printf(" Problems with writing to '%s'.\n", cornerListFileName);

    freeMem;
    printf("======================================================\n\n");

    return 0;

}
/*============================================================================*/



